/*
 * Copyright (C) 2015-2021 Alibaba Group Holding Limited
 */

#include <stdio.h>
#include <stdlib.h>
#include <ulog/ulog.h>
#include <dirent.h>
#include "cx_debug.h"
#include "cx_record.h"
#include "record.h"

#define TAG "cxrecord"

static cx_record_priv_t *record_hdl;
void *cx_vr_init(cx_record_param_t *param)
{
    CHECK_PARAM(param && param->store_path, NULL);

    cx_record_priv_t *record_priv = (cx_record_priv_t *)malloc(sizeof(cx_record_priv_t));
    CHECK_RET_WITH_RET(record_priv, NULL);
    memset(record_priv, 0, sizeof(cx_record_priv_t));

    record_priv->root_path = (char *)calloc(1, strlen(param->store_path) + 1);
    CHECK_RET_WITH_RET(record_priv->root_path, NULL);

    memcpy(record_priv->root_path, param->store_path, strlen(param->store_path));
    record_priv->save_type = param->save_type;

    if (access(record_priv->root_path, 0) != 0) {
        mkdir(record_priv->root_path, 755);
        LOGD(TAG, "create dir %s\n", record_priv->root_path);
    }

    cx_vr_start_stmanager(record_priv);
    record_hdl = record_priv;
    return record_priv;
}

int cx_vr_deinit(void *hdl)
{
    CHECK_PARAM(hdl, -1);

    cx_record_priv_t *priv = (cx_record_priv_t *)hdl;
    free(priv);
    record_hdl = NULL;

    return 0;
}

void *cx_record_get_handle(void)
{
    return record_hdl;
}
